/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.tarifador.view;

import br.com.tarifador.controller.PabxRN;
import br.com.tarifador.dataModel.PabxTableModel;
import br.com.tarifador.model.Pabx;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.HibernateException;
import org.primefaces.context.RequestContext;

/**
 *
 * @author charles
 */
@ManagedBean
@SessionScoped
public class PabxBean {

    private PabxRN pabxRN = new PabxRN();
    private List<Pabx> listaPABX;
    private Pabx pabxSelected;
    private PabxTableModel pabxModel;

    public void salvaPABX() {
        boolean salvar = false;
        try {
            salvar = pabxRN.salvaPABX();
        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        if (salvar == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaPabx();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void listaPabx() {
        listaPABX = new ArrayList<Pabx>();
        try {
            listaPABX = pabxRN.listaDePABX();
        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        this.pabxModel = new PabxTableModel(listaPABX);
    }

    public void alteraPabx() {
        boolean valida = false;
        try {
            valida = pabxRN.updatePABX(pabxSelected);

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaPabx();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));

        }
    }

    public void deletePabx() {
        boolean valida = false;
        try {
            valida = pabxRN.deletaPABX(pabxSelected.getId());
        } catch (NullPointerException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Selecione um campo!", ""));
        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao deletar os dados!\n " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao deletar os dados!\n " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao deletar dados em brancos!", ""));
        } else {
            listaPabx();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void btnAlterarPabx() {
        RequestContext request = RequestContext.getCurrentInstance();
        try {
            if (pabxSelected.getId() != null && pabxSelected.getId() > 0) {
                request.addCallbackParam("sucesso", true);
            } else {
                request.addCallbackParam("sucesso", false);
            }
        } catch (NullPointerException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Selecione um campo!", ""));
        }
    }

    public void limpaLista() {
        if (pabxModel != null) {
            listaPABX.clear();
            pabxModel = new PabxTableModel(listaPABX);
        }
    }

    public List<Pabx> getListaPABX() {
        return listaPABX;
    }

    public void setListaPABX(List<Pabx> listaPABX) {
        this.listaPABX = listaPABX;
    }

    public PabxTableModel getPabxModel() {
        return pabxModel;
    }

    public void setPabxModel(PabxTableModel pabxModel) {
        this.pabxModel = pabxModel;
    }

    public PabxRN getPabxRN() {
        return pabxRN;
    }

    public void setPabxRN(PabxRN pabxRN) {
        this.pabxRN = pabxRN;
    }

    public Pabx getPabxSelected() {
        return pabxSelected;
    }

    public void setPabxSelected(Pabx pabxSelected) {
        this.pabxSelected = pabxSelected;
    }
}
